package com.cloud.controller;

import com.alibaba.fastjson.JSON;
import com.cloud.dto.CloudLogRecordReqQuery;
import com.cloud.entity.CloudLogRecord;
import com.cloud.entity.SysLog;
import com.cloud.rest.PageInfoRes;
import com.cloud.rest.RestRes;
import com.cloud.service.CloudLogRecordService;
import com.cloud.utils.HBaseUtils;
import com.cloud.utils.SystemInfoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "日志接口")
@Slf4j
public class CloudLogRecordController {
    @Autowired
    private CloudLogRecordService cloudLogRecordService;
    @Autowired
    private HBaseUtils hbaseUtils;
    @Autowired
    private SystemInfoUtils systemInfoUtils;



    @ApiOperation(value = "查询日志记录logRecord")
    @PostMapping(value = "getCloudLogRecordPage")
    public RestRes<PageInfoRes<CloudLogRecord>> getCloudLogRecordPage(@RequestBody CloudLogRecordReqQuery cloudLogRecordReqQuery) {
        PageInfoRes<CloudLogRecord> cloudLogRecordPage = cloudLogRecordService.getCloudLogRecordPage(cloudLogRecordReqQuery);
        return RestRes.apiData(cloudLogRecordPage);
    }

    @ApiOperation(value = "批量插入接口（对外暴漏）")
//    @CloudLimitAnnotation()
    @PostMapping(value = "batchInsertLog")
    public RestRes<Void> batchInsertLog(@RequestBody List<SysLog> sysLog) {
        PageInfoRes<Void> cloudLogRecordPage = cloudLogRecordService.batchInsertLog(sysLog);
        return RestRes.apiData(cloudLogRecordPage);
    }

    @ApiOperation(value = "同步mysql日志记录到hbase")
    @GetMapping(value = "synHbase")
    public RestRes<Boolean> synHbase(@RequestParam("tableName") String tableName, @RequestParam("size") String size, @RequestParam("id") String id) {
        //防止浏览器自动重试，所以开启异步线程处理
        new Thread(() -> {
            try {
                cloudLogRecordService.synHbase(tableName, size, id);
            } catch (Exception e) {
                log.error("同步失败" + e);
            }
        }).start();
        return RestRes.apiData(true);
    }

    @ApiOperation(value = "Hbase创建表")
    @GetMapping(value = "createTable")
    public RestRes<Void> createTable(@RequestParam("tableName") String tableName) throws Exception {
        List<String> list = new ArrayList<>();
        list.add("logs");
        Boolean table = hbaseUtils.createTableAndCoprocessor(tableName, list);
        log.info("创建表状态：" + table);
        return RestRes.apiData(null);
    }

    @ApiOperation(value = "Hbase删除表")
    @GetMapping(value = "deleteTable")
    public RestRes<Void> deleteTable(@RequestParam("tableName") String tableName) throws Exception {
        List<String> list = new ArrayList<>();
        list.add("logs");
        Boolean table = hbaseUtils.deleteTable(tableName);
        log.info("创建表状态：" + table);
        return RestRes.apiData(null);
    }

    //只需要传递时间如20221109
    //如：http://localhost:8792/getAllCount?start=202211081642&stop=202211081646&tableName=cloud_log_record_202210
    @ApiOperation(value = "Hbase查询数量-多region")
    @GetMapping(value = "getAllCount")
    public RestRes<Long> getAllCount(@RequestParam("start") String start, @RequestParam("stop") String stop, @RequestParam("tableName") String tableName) {
        Long logSize = hbaseUtils.getAllCount(tableName, start, stop);
        log.info("查询所有region的满足条件的数量：" + logSize);
        return RestRes.apiData(logSize);
    }

    //需要传递如1-20221109   1-20221110
    //如：http://localhost:8792/getCount?start=1-202211091116&stop=1-202211091117&tableName=cloud_log_record_202210
    @ApiOperation(value = "Hbase查询数量-单region")
    @GetMapping(value = "getCount")
    public RestRes<Long> getCount(@RequestParam("start") String start, @RequestParam("stop") String stop, @RequestParam("tableName") String tableName) {
        Long logSize = hbaseUtils.getCount(tableName, start, stop);
        log.info("单个region满足条件的数量：" + logSize);
        return RestRes.apiData(logSize);
    }

    //如：http://localhost:8792/getRowCountByFilter
    //请求体：{"tableName":"cloud_log_record_202210","sourceApplicationName":"DemoA","reqStartTime":"2022-11-09 11:15","reqEndTime":"2022-11-09 11:17"}
    @ApiOperation(value = "Hbase查询数量-多region条件查询")
    @PostMapping(value = "getRowCountByFilter")
    public RestRes<Long> getRowCountByFilter(@RequestBody CloudLogRecordReqQuery cloudLogRecordReqQuery) {
        Long result = hbaseUtils.getAllCountByFilter(cloudLogRecordReqQuery);
        return RestRes.apiData(result);
    }

    //如：http://localhost:8792/getRow?rowKey=9-28101514184377499&tableName=cloud_log_record_202211
    @ApiOperation(value = "Hbase查询数据-rowkey查询")
    @GetMapping(value = "getRow")
    public RestRes<List<Map>> getRow(@RequestParam("rowKey") String rowKey, @RequestParam("tableName") String tableName) {
        List<Map> row = hbaseUtils.getRow(tableName, rowKey);
        return RestRes.apiData(row);
    }

    //如：http://localhost:8792/getRowByFilter
    //请求体：{"tableName":"cloud_log_record_202210","sourceApplicationName":"DemoB","reqStartTime":"2022-11-09 11:34","reqEndTime":"2022-11-09 11:37"}
    @ApiOperation(value = "Hbase查询数据-条件查询")
    @PostMapping(value = "getRowByFilter")
    public RestRes<List<Map>> getRowByFilter(@RequestBody CloudLogRecordReqQuery cloudLogRecordReqQuery) throws Exception {
        List<Map> row = hbaseUtils.selectByFilter(cloudLogRecordReqQuery);
        return RestRes.apiData(row);
    }

    @ApiOperation(value = "获取内存及磁盘相关信息")
    @GetMapping(value = "getMem")
    public RestRes<Map> getMem() {
        Map mem = systemInfoUtils.getMem();
        log.info(JSON.toJSONString(mem));
        return RestRes.apiData(mem);
    }
}
